package com.haier.cloudremote.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.haier.cloudremote.common.SLog;
import com.haier.cloudremote.model.KeyInfo;
import com.haier.cloudremote.model.StbInfo;

public class DbAdapter {

	private static final String DB_NAME = "ssc.db";
	private static final String TB_NAME = "tb_studykey";
	
	public static final String F_ID="_id";
	public static final String F_KEYCODE="keycode";
	public static final String F_KEYNAME="keyname";
	public static final String F_STATE="state";
	public static final String F_PAGENUM="pagenum";
	
	private Context ctx;
	private DbHelper dbHelper;
	private SQLiteDatabase db;
	
	public DbAdapter(Context ctx){
		this.ctx = ctx;
	}	
	
	public DbAdapter open(){
		dbHelper = new DbHelper(ctx);
		db = dbHelper.getWritableDatabase();
		return this;
	}
	
	public void close(){
		dbHelper.close();
	}
	
	public Cursor getAll(){
		String col[] = new String[]{F_ID,F_KEYCODE,F_KEYNAME,F_STATE,F_PAGENUM};
		return db.query(TB_NAME, col, null, null, null, null, null);
	}
	
	public long insertKey(KeyInfo keyInfo){
		ContentValues initValues = new ContentValues();
		initValues.put(F_KEYCODE, keyInfo.getKeyCode());
		initValues.put(F_KEYNAME, keyInfo.getKeyName());
		initValues.put(F_STATE, keyInfo.getState());
		initValues.put(F_PAGENUM, keyInfo.getPageNum());
		return db.insert(TB_NAME, null, initValues);
		
	}
	
	public int deleteKey(long keycode){
		return db.delete(TB_NAME, F_KEYCODE+"="+keycode, null);
	}
	
	public Cursor getKeyOfPage(int pageNum){
		String col[] = new String[]{F_ID,F_KEYCODE,F_KEYNAME,F_STATE,F_PAGENUM};
		//return db.rawQuery("select * from tb_diary",null);
		return db.query(TB_NAME, col, F_PAGENUM+"="+pageNum, null, null, null, null);
	}
	
	public Cursor getkey(int keycode){
		String col[] = new String[]{F_ID,F_KEYCODE,F_KEYNAME,F_STATE};
		return db.query(TB_NAME, col, F_KEYCODE+"="+keycode, null, null, null, null);
	}
	
	/**
	 * 修改学习状态，初始为未学习，此方法将指定按键修改为已学习
	 * @param keycode
	 * @param state
	 * @return
	 */
	public int changeState(int keycode, int state){
		ContentValues initValues = new ContentValues();
		initValues.put(F_STATE, state);
		return db.update(TB_NAME,initValues,F_KEYCODE+"="+keycode,null);
	}
	
	/**
	 * 初始化学习状态
	 */
	public int initStudyState(){
		ContentValues initValues = new ContentValues();
		initValues.put(F_STATE, 0);
		return db.update(TB_NAME,initValues,null,null);
	}
	
	// auto increment	
	private static class DbHelper extends SQLiteOpenHelper{
		
		public DbHelper(Context context) {
			super(context, DB_NAME, null, 3);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			String createSql = "create table " +TB_NAME+
					"( " +
					F_ID+" integer primary key AUTOINCREMENT," +
					F_KEYCODE +" integer not null," +
					F_KEYNAME +" text not null," +
					F_STATE+" integer not null," +
					F_PAGENUM+" integer" +
					")";
			SLog.d("ssc","create sql:"+createSql);
			db.execSQL(createSql);
			
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
			// TODO Auto-generated method stub
			db.execSQL("drop table if exists "+TB_NAME);
			onCreate(db);
		}
		
	}
	
}
